# coding: UTF-8
# Filename : httputils.py
# Company : imedia
# Author : rose
# -*-coding:utf-8 -*-
from django.http import HttpResponse
import log,utils,const

#密钥
key = '%imedia#2011'

#做验证post请求
def do_check(request, md5_param, secret):
    
    if md5_param == None or md5_param.strip() == '' or secret == None or secret.strip() == '': return False
    
    check_value = post(request, md5_param.strip())
    check_value = check_value.strip()
    md5 = utils.getMD5(check_value + key)
    md5_last = md5[-8:]
    secret = secret.strip()
    if secret == md5_last: return True
    else: return False

#做验证get请求
def do_check_get(request, md5_param, secret):
    
    if md5_param == None or md5_param.strip() == '' or secret == None or secret.strip() == '': return False
    
    check_value = get(request, md5_param.strip())
    check_value = check_value.strip()
    md5 = utils.getMD5(check_value + key)
    md5_last = md5[-8:]
    secret = secret.strip()
    if secret == md5_last: return True
    else: return False

#获取get请求参数
def get(request, param):
    if param == None or param.strip() == '': return ''
    if not request.GET.has_key(param): return '';
    return request.GET[param]


#获取post请求参数
def post(request, param):
    if param == None or param.strip() == '': return ''
    if not request.POST.has_key(param): return '';
    return request.POST[param]

#获取所有参数
def getAllParam(request):
    rst = ''
    dict_post = request.POST
    for a,b in dict_post.items():
        rst_temp = ' | ' + str(a) + ':' + str(b)
        rst += rst_temp
    return rst

#记录访问轨迹
def footprint(request):
    ip = get_info(request, 'REMOTE_ADDR')
    ua = get_info(request, 'HTTP_USER_AGENT')
    reffer = get_info(request, 'HTTP_REFERER')
    url = request.build_absolute_uri()
    param = [ip, url, reffer, ua]
    if const.print_param:
        param.append(getAllParam(request))
    log.print_access_log('`'.join(param))


#获取请求头信息相关数据
def get_info(request, param):
    if request.META.has_key(param): return request.META[param]
    else: return 'empty'
        

    
